package com.backend.system.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import com.backend.system.common.RequestHolderHandlerInterceptor;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.*;

import java.util.List;

/**
 * MVC配置
 *
 * @version v1.0
 * @author: backend-manage
 * @date: 2024年02月26日 14时06分
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Value("${server.servlet.context-path}")
    private String servletContextPath;

    @Bean
    public HandlerInterceptor handlerInterceptor() {
        return new RequestHolderHandlerInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<InterceptorRegistration> registrations = Lists.newArrayList();
        registrations.add(registry.addInterceptor(handlerInterceptor()));
        registrations.add(registry.addInterceptor(new SaInterceptor()));

        for (InterceptorRegistration registration : registrations) {
            registration.addPathPatterns("/**");
            registration.excludePathPatterns(servletContextPath);
            registration.excludePathPatterns(
                    "/rpc/**",
                    "/api/**",
                    "/ct/**",
                    "/test/**",
                    "/static/**",
                    "/favicon.ico",
                    "/static/favicon.ico",
                    "/swagger-resources/**",
                    "/swagger-ui.html/**",
                    "/webjars/**",
                    "/doc.html",
                    "/doc.html#/**",
                    "/v2/api-docs/**",
                    "/sys/user/doRegister",
                    "/sys/user/doLogin"
            );
        }
    }

    /**
     * 处理跨域
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/META-INF/resources/");
    }
}
